package org.ch02.job.test11;

import java.lang.reflect.Field;
import java.util.Map;

/**
 * @author XCH
 * @date 2025 - 03 - 19
 */
public class BuildFull {
    public static String buildFullSql(String sql, User user) {
        StringBuilder fullSql = new StringBuilder(sql);
        try {
            // 获取 User 类的所有字段
            Field[] fields = User.class.getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true); // 设置字段可访问
                String fieldName = field.getName();
                Object value = field.get(user); // 获取字段值

                // 替换 SQL 中的 ? 为实际值
                int paramIndex = fullSql.indexOf("?");
                if (paramIndex != -1) {
                    if (value instanceof String) {
                        fullSql.replace(paramIndex, paramIndex + 1, "'" + value + "'");
                    } else {
                        fullSql.replace(paramIndex, paramIndex + 1, value.toString());
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return fullSql.toString();
    }
}
